package com.kye.express.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;

import com.kye.express.R;
import com.kye.express.view.ToastManager;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 27/01/2018
 * @updated : 27/01/2018
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

public class PhoneUtils {

    public static void call(Activity activity, String phone) {
        Observable.just(new RxPermissions(activity))
                .flatMap(new Function<RxPermissions, ObservableSource<Permission>>() {
                    @Override
                    public ObservableSource<Permission> apply(RxPermissions rxPermissions) throws Exception {
                        return rxPermissions.requestEach(Manifest.permission.CALL_PHONE);
                    }
                })
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) throws Exception {
                        if (permission.granted) {
                            Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            activity.startActivity(intent);
                        } else {
                            ToastManager.show(activity, R.string.personal_commissioner_reject_permissions);
                        }
                    }
                });
    }
}
